package com.example.combwyaq.mvp.presenter;

import android.util.Log;

import com.example.combwyaq.mvp.model.entity.BaseEntity;
import com.example.combwyaq.mvp.model.IModel;
import com.example.combwyaq.mvp.viwe.IView;

import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract class BasePresenter<M extends IModel,V extends IView> implements IPresenter, Observer<BaseEntity> {
    private String TAG="yqs";
    private CompositeDisposable compositeDisposable;/*RxJava下专门用来保存*/
     protected  M m;
     protected  V v;
//    RxJava在订阅成功后disposable参数的，当异步任务未执行完成，且界面被用户手动关闭
//     在通过lifecycyle关联的声明周期方法onDestroy中接触rxjava异步订阅任务防止内存泄漏


    public BasePresenter(M m, V v) {
        Log.i(TAG, "BasePresenter: ");
        this.m = m;
        this.v = v;
        compositeDisposable=new CompositeDisposable();
    }

    @Override
    public void Destroy() {
        Log.i(TAG, "Destroy: ");
        /*解除全部异步任务*/
        compositeDisposable.dispose();
        compositeDisposable.clear();

    }

    @Override
    public void onSubscribe(Disposable d) {
        Log.i(TAG, "onSubscribe: ");
        compositeDisposable.add(d);
        v.showDialog();
    }



    @Override
    public void onError(Throwable e) {
        v.showMsg(e.getMessage());
    }

    @Override
    public void onComplete() {
        v.hideDialog();
    }
}
